import java.util.*;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {

        Set<ListNode> set=new HashSet<>();

        ListNode cur1=pHead1;
        ListNode cur2=pHead2;

        while(cur1!=null){
            set.add(cur1);
            cur1=cur1.next;
        }

        while(cur2!=null){
            if(!set.add(cur2)){
                break;
            }
            cur2=cur2.next;
        }
        return cur2;


    }
}
